package com.hb.trade.server.project.section.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.enums.SectionStatusEnum;
import com.hb.core.pojo.project.section.dto.BidSectionTenderDTO;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.core.pojo.project.section.entity.BidSectionTender;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import com.hb.trade.server.project.section.service.raw.BidSectionTenderService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.MsgUtil;

import java.util.List;

/**
 * 投标人报名操作类
 *
 * @author zhaojk
 */
public class BidSectionTenderManager extends BidSectionTenderDTO {

    /**
     * 投标文件递交验证
     *
     * @return
     */
    public Message<Boolean> submitValid() {
        Long sectionId = getSectionId();
        Long orgId = getOrgId();
        if (null == sectionId) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        Attachments tenderDocument = getTenderDocument();
        if (CollUtil.isEmpty(tenderDocument)) {
            return MsgUtil.fail("请先上传投标文件");
        }

        //判断标段状态，只有标段状态为 NOTICE_ISSUED(1465922770142670849L, 146, "项目公告已发布") 时才允许继续投标
        BidSectionService bidSectionService = SpringUtil.getBean(BidSectionService.class);
        BidSection bidSection = bidSectionService.getById(sectionId);
        if (null == bidSection) {
            return MsgUtil.fail("未获取到标段信息");
        }
        SectionStatusEnum ins = SectionStatusEnum.getIns(bidSection.getSectionStatus());
        if (SectionStatusEnum.BID_FILE_SOLD != ins) {
            return MsgUtil.fail("当前标段状态不允许继续投标");
        }
        //判断当前标段、投标人企业是否已递交投标文件
        BidSectionTenderService bidSectionTenderService = SpringUtil.getBean(BidSectionTenderService.class);
        List<BidSectionTender> sectionTenderInfoByOrgId = bidSectionTenderService.findSectionTenderInfoByOrgId(orgId, sectionId);
        if (CollUtil.isNotEmpty(sectionTenderInfoByOrgId)){
            return MsgUtil.fail("当前标段已被本公司其他员工递交投标文件，请勿重复递交");
        }


        return null;
    }
}
